﻿<?php
/*
 * Created on Feb 7, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 	$title = 'Tìm kiếm bất động sản';
 	include('../includes/session.php');
	if($session->logged_in){
		if($session->userlevel == 1){
			header("Location: ../index.php");
		}
	}else{
		header("Location: login.php");
	}
	include('../includes/encrypturl.php');
	include('../models/cityregion_model.php');
	include('../models/transaction_model.php');
	include('../models/realestate_model.php');
	include('../models/reesnew_model.php');
	$session->setHeader(9);	
 	include('../includes/header.inc');
	
	echo '<script type="text/javascript" src="../js/prototype-1.6.0.2.js"></script>';
		
	echo '<div id="wrapper">';
	echo '<div id="content">';
	if(count($_GET) > 0){
		echo '<div id="box">';
		
		$start=$_GET['start'];
		if(strlen($start) > 0 and !is_numeric($start)){
			echo "Data Error";
			exit;
		}
		$eu = ($start - 0);
		$limit = $number_record_perpage;
		$this1 = $eu + $limit;
		$back = $eu - $limit;
		$next = $eu + $limit;
		$num = 0;
		$stringGet = '';
		foreach($_GET as $key => $value){
			if(!empty($value) && $key != 'start')
				$stringGet .= '&'.$key.'='.$value;
		}
		$resultSearch = $reesNewModel->searchAdmin($_GET, $num, $eu, $limit);		
	    echo '<h3>Kết quả tìm kiếm</h3>';
		echo '<table width="100%" style="font-size: 11px;"><thead>';
			echo '<tr class="borderBottom">';
	        echo '<th><a href="#">ID<img src="'.$PathPrefix.'img/icons/arrow_down_mini.gif" width="16" height="16" align="absmiddle" /></a></th>';
	        echo '<th><a href="#">Tiêu đề</a></th>';     
	        echo '<th><a href="#">Liên hệ</a></th>';
			echo '<th><a href="#">Giá bán/thuê</a></th>';
			echo '<th><a href="#">Lượt xem</a></th>';
	        echo '<th><a href="#">Ngày tạo</a></th>';
			echo '<th><a href="#">Ngày cập nhật</a></th>';
			echo '<th><a href="#">Ngày hết hạn</a></th>';
	        echo '<th><a href="#">Hành động</a></th>';
	        echo '</tr></thead>';
			while ($myrow = $reesNewModel->getArrayNameItem($resultSearch)){
				printf('<tbody><tr class="borderBottom">
						<td class="a-center">%s</td>						
	                    <td>%s</td>
	                    <td>%s</td>
	                    <td>%s</td>
						<td>%s</td>
						<td>%s</td>
	                    <td>%s</td>	
						<td>%s</td>
	                    <td><a href="%sSelectedRees=%s"><img src="'.$PathPrefix.'img/icons/user_edit.png" title="Cập nhật BĐS" width="16" height="16" /></a>
							<a href="%sSelectedRees=%s&delete=1"><img src="'.$PathPrefix.'img/icons/user_delete.png" title="Xóa BĐS" width="16" height="16" /></a></td>
	                    </tr></tbody>',
						$myrow['id'],
						$myrow['title'],
						$myrow['contact'],
						$myrow['price'],
						$myrow['showNo'],
						ConvertSQLDate($myrow['createDate']),
						ConvertSQLDate($myrow['updateDate']),
						ConvertSQLDate($myrow['endDate']),	
						'managerees.php' . '?' . SID . '&',
						$myrow['id'],
						'managerees.php' . '?' . SID . '&',
						$myrow['id']);				
			}			
		echo '</table>';
		if($num > $limit ){
			echo '<div id="pager">Trang ';
			if($back >=0) {  
				echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$back.$stringGet.'"><img src="'.$PathPrefix.'img/icons/arrow_left.gif" width="16" height="16" /></a>';
			};
			$i=0;
			$l=1;
			for($i=0;$i < $num;$i=$i+$limit){
				if($i <> $eu){
					echo " <a href='".$_SERVER['PHP_SELF']."?start=$i.$stringGet'><font face='Verdana' size='2'>$l</font></a> ";
				}
				else { 
					echo "<font face='Verdana' size='4' color=red>$l</font>";				
				}
					$l=$l+1;
			}
			if($this1 < $num) {
				echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$next.$stringGet.'"><img src="'.$PathPrefix.'img/icons/arrow_right.gif" width="16" height="16" /></a>';
			};
            
            echo ' Tổng cộng <strong>'.$num.'</strong> bất động sản </div>';
		}else{
			echo '<div id="pager">Tổng cộng <strong>'.$num.'</strong> bất động sản</div>';
		}
	
	
		echo '</div>';
		echo '<br />';
	}
	echo '<div id="box">';
		
	echo "<form id='form' method='post' action='../controllers/reesitem_controller.php" . "?" . SID . "'>";
	echo '<h3 id="adduser">Tìm Bất động sản</h3><br/>';
	echo '<fieldset id="personal">';
    echo '<legend>THÔNG TIN TÌM KIẾM</legend>';
	
	echo '<label for="title">Tiêu đề: </label>
		<input name="title" id="title" type="text" tabindex="1" size="70" value="'.$_GET['title'].'"/><br />';
	echo '<label for="address">Địa chỉ: </label>
		<input name="address" id="address" type="text" tabindex="1" size="70" value="'.$_GET['address'].'"/><br />';
	echo '<label for="size">Diện tích (m2)</span>: </label>
		<input name="size" id="size" type="text" tabindex="1" size="70" value="'.$_GET['size'].'"/><br />';
	echo '<label for="source">Nguồn tin: </label>
		<input name="source" id="source" type="text" tabindex="1" size="70" value="'.$_GET['source'].'"/><br />';
	echo '<label for="direction">Hướng nhà: </label>
		<input name="direction" id="direction" type="text" tabindex="1" size="70" value="'.$_GET['direction'].'"/><br />';
	echo '<label for="price">Giá bán/thuê (triệu/VNĐ): </label>
		<input name="price" id="price" type="text" tabindex="1" size="70" value="'.$_GET['price'].'"/><br />';
	echo '<label for="description">Mô tả: </label>';
		echo '<textarea type="text" name="description" cols="5" rows="1">'.$_GET['description'].'</textarea><br />';
	echo '<label for="content">Nội dung: </label>';
		echo '<textarea type="text" name="content" cols="5" rows="1">'.$_GET['content'].'</textarea><br />';
	echo '<label for="contact">Liên hệ - điện thoại: </label>';
		echo '<textarea type="text" name="contact" cols="5" rows="1">'.$_GET['contact'].'</textarea><br />';
		
	$trans = $transactionModel->getTrans(null, null);
	echo '<label for="reestype">Loại tin: </label>';							
	echo '<select name="reestype" class="selectOneMedium" style="font-size: 11px;">';
		echo '<option value="0">- Tất cả -</option>';		
		while ($myrow = $transactionModel->getArrayNameTrans($trans)) {
			if($_GET['reestype'] == $myrow['id'])
				echo '<option selected="selected" value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
			else
				echo '<option value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
		}
	echo '</select><br />';
	$typehouse = $realestateModel->getTypes(null, null);	
	echo '<label for="typehouse">Loại nhà: </label>';
	echo '<select name="typehouse" class="selectOneMedium" style="font-size: 11px;">';
		echo '<option value="0">- Tất cả -</option>';		
		while ($myrow = $realestateModel->getArrayNameTypes($typehouse)) {
			if($_GET['typehouse'] == $myrow['id'])
				echo '<option selected="selected" value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
			else 
				echo '<option value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
		}
	echo '</select><br />';
	$region = $cityModel->getRegions(null, null);
	echo '<label for="region">Khu đô thị:</label>';
	echo '<select name="region" class="selectOneMedium" style="font-size: 11px;">';
		echo '<option value="0">- Tất cả -</option>';		
		while ($myrow = $cityModel->getArrayNameRegions($region)) {
			if($_GET['region'] == $myrow['id'])
				echo '<option selected="selected" value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
			else 
				echo '<option value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
		}
	echo '</select><br />';
	$cities = $cityModel->getCities();
	echo '<label for="city">Tỉnh/TP:</label>';
	echo '<select name="city" class="selectOneMedium" onchange="cityChange(this.value)" style="font-size: 11px;">';
		echo '<option value="0">- Tất cả -</option>';		
		while ($myrow = $cityModel->getArrayNameRegions($cities)) {
			if($_GET['city'] == $myrow['id'])
				echo '<option selected="selected" value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
			else 
				echo '<option value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
		}
	echo '</select><br />';	
	echo '<label for="district">Quận/Huyện :</label>';
	echo '<select name="district" id ="district" class="selectOneMedium" style="font-size: 11px;">';
		echo '<option value="0">- Chọn Tỉnh/TP -</option>';
		if($_GET['city'] > 0){
			$districts = $cityModel->getDistrictsByCity($_GET['city']);			
			while ($myrow = $cityModel->getArrayNameRegions($districts)) {
				if($_GET['district'] == $myrow['id'])
					echo '<option selected="selected" value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
				else 
					echo '<option value="'. $myrow['id'] .'">'. $myrow['name'] .'</option>';
			}
		}
	echo '</select><br />';
	
	echo '<label for="createFromDate">Ngày tạo: Từ </label><input name="createFromDate" id="createFromDate" readonly="readonly" type="text" tabindex="7" size="15" value="'.$today.'"/>';
		echo '<a href="javascript:NewCal(\'createFromDate\', \'ddmmyyyy\', false, 24, \'..\');"><img border="0" src="../img/cal.jpg" alt="" /></a>';
	echo '  Đến <input name="createToDate" id="createToDate" readonly="readonly" type="text" tabindex="7" size="15" value="'.$today.'"/>';
		echo '<a href="javascript:NewCal(\'createToDate\', \'ddmmyyyy\', false, 24, \'..\');"><img border="0" src="../img/cal.jpg" alt="" /></a><br />';	
	
	echo '<input type="hidden" name="itemsearchadmin" value="1" />';
	echo '</fieldset>';
	echo '<div align="center">
     		<input name="submit" type="submit" value="Đồng ý" /> 
            <input name="reset" type="reset"  value="Thiết lập lại"/>
          </div></form>';
	
	echo '</div>';
	echo '</div>';
	include('../includes/footer.inc');
?>
<script>
	function cityChange(value){
		var url = '../getDistrict.php?cityId=';
		new Ajax.Request(url + value,
		  {
		    method:'get',
		    onSuccess: function(transport){
				var cbxDistrict = document.getElementById('district');
				cbxDistrict.options.length = 0;
				cbxDistrict.options[0] = new Option('- Tất cả -', 0);
				var response = transport.responseText;
				var districts = eval( "(" + response + ")");
				for(i =0 ; i < districts.length; i ++)					
					cbxDistrict.options[cbxDistrict.options.length] = new Option(districts[i]['name'], districts[i]['id']);
		    },
		    onFailure: function(){ alert('Something went wrong...') }
		  });
	}
</script>
